﻿using BLL;
using IBLL;
using Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using user_defined_Panl;

namespace 考试管理系统
{
    public partial class Form1 : Form
    {
        public static string path  = "";
        public static FlowLayoutPanel flowLayoutPanel1;
        public Panel panel1=null;
        public static int max = 0;
        public static int index = 0;
        IFrQuestionBankManage_IBLL frQuestion = new FrQuestionBankManage_BLL();
        public Form1()
        {
            InitializeComponent();
            uiTitlePanel1.Text = path;
            for (int i = 0; i < max; i++)
            {
                if (max==4&&i>2)
                {
                    break;
                }
                //if (i==2||i==5)
                //{
                //    NumericUpDown numericUpDown = new NumericUpDown();
                //    flowLayoutPanel2.Controls.Add(numericUpDown);
                //}
                //else
                //{
                    UITextBox uIText = new UITextBox();
                    uIText.Height = 50;
                    uIText.Multiline = true;
                    uIText.Width = 380;
                    uIText.AutoScroll = true;
                    flowLayoutPanel2.Controls.Add(uIText);
               // }
             
            };

            switch (max)
            {
                case 3:
                    (flowLayoutPanel2.Controls[0] as UITextBox).Watermark = "请输入题目";
                    flowLayoutPanel2.Controls[2].Width = 100;
                    flowLayoutPanel2.Controls[2].Height = 30;
                    (flowLayoutPanel2.Controls[2] as UITextBox).Watermark = "请输入分数";
                    flowLayoutPanel2.Controls[1].Width = 380;
                    flowLayoutPanel2.Controls[1].Height = 50;
                    (flowLayoutPanel2.Controls[1] as UITextBox).Watermark = "请输入答案";
                    break;
                case 4:
                    flowLayoutPanel2.Controls[0].Height = 100;
                    (flowLayoutPanel2.Controls[0] as UITextBox).Watermark = "请输入题目";
                    flowLayoutPanel2.Controls[2].Width = 100;
                    flowLayoutPanel2.Controls[2].Height = 30;
                    (flowLayoutPanel2.Controls[2] as UITextBox).Watermark = "请输入分数";   
                    flowLayoutPanel2.Controls[1].Width = 380;
                    flowLayoutPanel2.Controls[1].Height = 50;
                    (flowLayoutPanel2.Controls[1] as UITextBox).Watermark = "请输入答案";
                    UIButton uIButton=new UIButton();
                      uIButton.Text = "添加得分点";
                    uIButton.Click += UIButton1_Click;
                    UIButton uIButton1 = new UIButton();
                    uIButton1.Text = "删除得分点";
                    uIButton1.Click += UIButton2_Click1;
                    flowLayoutPanel2.Controls.Add(uIButton);
                    flowLayoutPanel2.Controls.Add(uIButton1);
                    ; break;
                case 7:
                    flowLayoutPanel2.Controls[0].Height = 100;
                    (flowLayoutPanel2.Controls[0] as UITextBox).Watermark = "请输入题目";
                    (flowLayoutPanel2.Controls[1] as UITextBox).Watermark = "请输入选项A";
                    (flowLayoutPanel2.Controls[2] as UITextBox).Watermark = "请输入选项B";
                    (flowLayoutPanel2.Controls[3] as UITextBox).Watermark = "请输入选项C";
                    (flowLayoutPanel2.Controls[4] as UITextBox).Watermark = "请输入选项D";
                     flowLayoutPanel2.Controls[5].Width = 100;
                     flowLayoutPanel2.Controls[5].Height = 30;
                    (flowLayoutPanel2.Controls[5] as UITextBox).Watermark = "请输入分数";
                    flowLayoutPanel2.Controls[6].Width = 200;
                    flowLayoutPanel2.Controls[6].Height = 30;
                    (flowLayoutPanel2.Controls[6] as UITextBox).Watermark = "请输入答案选项(ABCD)";
                    ; break;
                default: break;
            }

        }

        private void UIButton2_Click1(object sender, EventArgs e)
        {
            if (flowLayoutPanel2.Controls[flowLayoutPanel2.Controls.Count-1] is FlowLayoutPanel)
            {
                flowLayoutPanel2.Controls.RemoveAt(flowLayoutPanel2.Controls.Count - 1);
            }
        }

        private void UIButton1_Click(object sender, EventArgs e)
        {
             FlowLayoutPanel flowLayoutPane3=new FlowLayoutPanel();
            flowLayoutPane3.Width = 380;
            flowLayoutPane3.Height = 40;
            UITextBox uITextBox = new UITextBox();
            uITextBox.Width = 300;
            uITextBox.Height = 30;
            uITextBox.Watermark = "请输入得分点";
            UITextBox uITextBox1 = new UITextBox();
            uITextBox1.Width = 60;
            uITextBox1.Height = 30;
            uITextBox1.Watermark = "请输入分数";
            flowLayoutPane3.Controls.Add(uITextBox);
            flowLayoutPane3.Controls.Add(uITextBox1);
            flowLayoutPanel2.Controls.Add(flowLayoutPane3);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void uiButton2_Click(object sender, EventArgs e)
        {
            string s = "";
          
            switch (path)
            {
                case "添加单选题":
                    index++;
                    s = $"insert into SingleChoice(Topic,A,B,C,D,Answer,Score) " +
                        $"values('{flowLayoutPanel2.Controls[0].Text}','{flowLayoutPanel2.Controls[1].Text}'," +
                        $"'{flowLayoutPanel2.Controls[2].Text}','{flowLayoutPanel2.Controls[3].Text}','{flowLayoutPanel2.Controls[4].Text}','{flowLayoutPanel2.Controls[6].Text}','{flowLayoutPanel2.Controls[5].Text}') select @@IDENTITY";
                    string s1 = flowLayoutPanel2.Controls[6].Text;
                    s1 = s1.ToUpper();
                    if (Regex.IsMatch(s1, @"[^ABCDabcd]"))
                    {
                        MessageBox.Show("请输入正确的答案：ABCD 之间");
                        return;
                    }
                    flowLayoutPanel1.Controls.Add(new FlowLayoutPanel_2(new MultipleChoice()
                    {
                        MCID = frQuestion.Query_ID(s).ToString(),
                        Topic = flowLayoutPanel2.Controls[0].Text,
                        Answer = flowLayoutPanel2.Controls[6].Text,
                        A = flowLayoutPanel2.Controls[1].Text,
                        B = flowLayoutPanel2.Controls[2].Text,
                        C = flowLayoutPanel2.Controls[3].Text,
                        D = flowLayoutPanel2.Controls[4].Text,
                        Score = flowLayoutPanel2.Controls[5].Text,
                    }, (flowLayoutPanel1.Controls.Count+1).ToString(), true, false)); break;
                case "添加多选题":
                    s = $"insert into MultipleChoice(Topic,A,B,C,D,Answer,Score) " +
                      $"values('{flowLayoutPanel2.Controls[0].Text}','{flowLayoutPanel2.Controls[1].Text}'," +
                      $"'{flowLayoutPanel2.Controls[2].Text}','{flowLayoutPanel2.Controls[3].Text}','{flowLayoutPanel2.Controls[4].Text}','{flowLayoutPanel2.Controls[6].Text}','{flowLayoutPanel2.Controls[5].Text}') select @@IDENTITY";
                    string s2 = flowLayoutPanel2.Controls[6].Text;
                   s2= s2.ToUpper();
                    if (Regex.IsMatch(s2, @"[^ABCDabcd]"))
                    {
                        MessageBox.Show("请输入正确的答案：ABCD 之间");
                        return;
                    }
                    flowLayoutPanel1.Controls.Add(new FlowLayoutPanel_2(new MultipleChoice()
                    {
                        MCID = frQuestion.Query_ID(s).ToString(),
                        Topic = flowLayoutPanel2.Controls[0].Text,
                        Answer = flowLayoutPanel2.Controls[6].Text,
                        A = flowLayoutPanel2.Controls[1].Text,
                        B = flowLayoutPanel2.Controls[2].Text,
                        C = flowLayoutPanel2.Controls[3].Text,
                        D = flowLayoutPanel2.Controls[4].Text,
                        Score = flowLayoutPanel2.Controls[5].Text,
                    }, (flowLayoutPanel1.Controls.Count+1).ToString(), true, false)); break;
                case "添加判断题":
                    s = $"insert into TrueOrFalseQuestions(Topic,Answer,Score) " +
                     $"values('{flowLayoutPanel2.Controls[0].Text}','{flowLayoutPanel2.Controls[1].Text}','{flowLayoutPanel2.Controls[2].Text}') select @@IDENTITY";
                    string s3 = flowLayoutPanel2.Controls[1].Text;
                    s3=s3.ToLower();
                    if (s3!="true"&& s3!="false")
                    {
                        MessageBox.Show("请输入正确的答案：true或false");
                        return;
                    }
                    flowLayoutPanel1.Controls.Add(new FlowLayoutPanel_3(new TrueOrFalseQuestions()
                    {
                        SPID = frQuestion.Query_ID(s).ToString(),
                        Topic = flowLayoutPanel2.Controls[0].Text,
                        Answer = flowLayoutPanel2.Controls[1].Text,
                        Score = flowLayoutPanel2.Controls[2].Text,
                    }, (flowLayoutPanel1.Controls.Count+1).ToString()));
                    break;
                case "添加填空题":
                    string s4 = "";
                    foreach (var item in flowLayoutPanel2.Controls)
                    {
                        if (item is FlowLayoutPanel)
                        {
                           if ((item as FlowLayoutPanel).Controls[0].Text.Trim()==""|| (item as FlowLayoutPanel).Controls[1].Text.Trim()=="")
                            {
                                MessageBox.Show("请检查得分点格式");
                                return;
                            }
                           s4+=(item as FlowLayoutPanel).Controls[0].Text.Trim()+":"+ (item as FlowLayoutPanel).Controls[1].Text.Trim()+" ";
                        }
                    }
                    s4=  s4.Trim();
                    s4= s4.Replace(" ", ";");
                    s = $"insert into Completion(Topic,Answer,Score,ScoringPoint) " +
                        $"values('{flowLayoutPanel2.Controls[0].Text}','{flowLayoutPanel2.Controls[1].Text}','{flowLayoutPanel2.Controls[2].Text}','{s4}') select @@IDENTITY";
                    flowLayoutPanel1.Controls.Add(new FlowLayoutPanel_4(new Completion()
                    {
                        CPID = frQuestion.Query_ID(s).ToString(),
                        Topic = flowLayoutPanel2.Controls[0].Text,
                        Answer = flowLayoutPanel2.Controls[1].Text,
                        Score = flowLayoutPanel2.Controls[2].Text,
                        ScoringPoint =s4
                    },( flowLayoutPanel1.Controls.Count + 1).ToString())); break;

                case "修改单选题":s = ""; panel1 = new Panl_1(flowLayoutPanel1); break;
                case "修改多选题": s = ""; panel1 = new Panl_2(flowLayoutPanel1); break;
                case "修改判断题":s = ""; panel1 = new Panl_3(flowLayoutPanel1); break;
                case "修改填空题": s = ""; panel1 = new Panl_4(flowLayoutPanel1); break;
                default:
                    break;
            }
            
        
        }
    }
}
